<?php
declare (strict_types=1);

namespace app\exam\service\admin;

use app\common\service\BaseService;
use app\exam\logic\admin\{QuestionBank, QuestionDifficult, QuestionPoint};
use app\exam\logic\admin\QuestionBankTopic as QuestionBankTopicLogic;
use app\exam\logic\admin\QuestionTopic as QuestionTopicLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * Class QuestionBankTopic
 *
 * @package app\exam\service\admin
 */
class QuestionBankTopic extends BaseService
{
    /**
     * @Inject()
     *
     * @var QuestionBankTopicLogic
     */
    protected $questionBankTopicLogic;

    /**
     * @Inject()
     *
     * @var QuestionTopicLogic
     */
    protected $questionTopicLogic;

    /**
     * @Inject()
     *
     * @var QuestionDifficult
     */
    protected $questionDifficult;

    /**
     * @Inject()
     *
     * @var QuestionPoint
     */
    protected $questionPoint;

    /**
     * 题库中的试题列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/22
     *
     * @param int $bankId
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function topics(int $bankId)
    {
        $map = [];
        // 关键字
        Request::has('title', 'get', true) && $map[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        // 知识点
        $point = Request::get('point/d', 0);
        $point && $map[] = ['point', 'like', '%,' . $point . ',%'];
        // 题型
        if (Request::has('question_type', 'get', true) && Request::get('question_type/d', -1) !== -1) {
            $map[] = ['question_type', '=', Request::get('question_type/d')];
        }
        // 难易度
        $difficulty = Request::get('difficulty/d', 0);
        $difficulty && $map[] = ['difficulty', '=', $difficulty];

        $data = $this->questionBankTopicLogic->getTopicListByBankId($bankId, $map)->append(['question_type_text']);

        // 定义字段
        $keyList      = [
            'title'              => ListItem::make('custom')->title('题目名称'),
            // 'question_bank_title' => ListItem::make()->title('所属题库'),
            'question_type_text' => ListItem::make()->title('题目类型'),
            'difficulty_title'   => ListItem::make()->title('试题难度'),
            'point_str'          => ListItem::make()->title('知识点'),
            'num'                => ListItem::make('custom')->title('被引用次数'),
            'answer_num'         => ListItem::make()->title('作答次数'),
            'update_time'        => ListItem::make()->title('更新时间')
        ];
        $questionType = $this->questionTopicLogic->getQuestionTypeOptions();
        $difficulty   = $this->questionDifficult->cascader();
        $category     = app(QuestionBank::class)->getCategory($bankId);
        $point        = $this->questionPoint->cascader(0, [['category', '=', $category]]);
        // 搜索字段
        $searchKey = [
            'title'         => FormItem::make('input')->title(false)->placeholder('题目名称')->maxlength(30),
            'question_type' => FormItem::make('select')->title(false)->placeholder('题目类型')->options($questionType),
            'difficulty'    => FormItem::make('select')->title(false)->placeholder('难易度')->options($difficulty),
            'point'         => FormItem::make('cascader')->title(false)->placeholder('知识点')->options($point),

        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey   = $keyList;
        $list->datas     = $data;
        $list->searchKey = $searchKey;

        return $list->send();
    }

    /**
     * 检查导入数据
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/5/17
     *
     * @return \think\Response
     */
    public function checkImport()
    {
        $attachId = Request::post('attach_id');
        $ret      = $this->questionBankTopicLogic->checkImport($attachId);

        return BaseService::parseToData($ret, 1);
    }

    /**
     * 试题导入
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/30
     *
     * @param int $bankId
     * @return \think\Response
     */
    public function import(int $bankId)
    {
        $attachId     = Request::post('attach_id');
        $successCount = $this->questionBankTopicLogic->doImport($bankId, $attachId);

        return $this->parseToData(['success' => $successCount], 1, '导入成功');
    }
}